//
//  ServiceMercTableViewCell.swift
//  kxf
//
//  Created by Mac on 2024/3/12.
//

import Foundation
import UIKit

class ServiceMercTableViewCell: UITableViewCell {
    
    var peizhiBlock:(()->())?
    var manageSettleBlock:(()->())?
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        initUI()
    }
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.layer.cornerRadius = kFitWidth(14)
        vi.clipsToBounds = true
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        return vi
    }()
    lazy var nameLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .regular)
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var timeLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        
        return lab
    }()
    lazy var statusBgView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .THEME
        vi.layer.cornerRadius = kFitWidth(14)
        vi.clipsToBounds = true
        
        return vi
    }()
    lazy var statusLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .white
        lab.font = .systemFont(ofSize: 12)
        lab.textAlignment = .center
        
        return lab
    }()
    lazy var manageSettleBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("管理结算价", for: .normal)
        btn.backgroundColor = .clear
        btn.titleLabel?.font = .systemFont(ofSize: 12, weight: .regular)
        btn.setTitleColor(.THEME, for: UIControl.State.normal)
        btn.layer.borderColor = UIColor.THEME.cgColor
        btn.layer.borderWidth = kFitWidth(1)
        btn.layer.cornerRadius = kFitWidth(4)
        btn.clipsToBounds = true
        btn.addTarget(self, action: #selector(manageSettleTapAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var bottomeView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        return vi
    }()
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .COLOR_LIGHT_GREY
        
        return vi
    }()
    
    lazy var mercPhoneLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK999
        lab.font = .systemFont(ofSize: 12)
        
        return lab
    }()
    lazy var peizhiBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("已配置", for: .normal)
        btn.setTitleColor(.THEME, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.isHidden = true
        return btn
    }()
    lazy var arrowImgView : UIImageView = {
        let img = UIImageView()
        img.setImgLocal(imgName: "login_arrow_img")
        img.isUserInteractionEnabled = true
        img.isHidden = true
        
        return img
    }()
    lazy var peizhiTapView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .clear
        vi.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(peizhiTapAction))
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
}

extension ServiceMercTableViewCell{
    @objc func peizhiTapAction() {
        if self.peizhiBlock != nil{
            self.peizhiBlock!()
        }
    }
    @objc func manageSettleTapAction() {
        if self.manageSettleBlock != nil{
            self.manageSettleBlock!()
        }
    }
    func updateUI(dict:NSDictionary) {
        nameLabel.text = "\(dict["name"]as? String ?? "")"
        timeLabel.text = "\(dict["createDate"]as? String ?? "")"
        mercPhoneLabel.text = "\(dict["phone"]as? String ?? "")"
        
        let status = dict["status"]as? Int ?? -5
        
        if status >= 1 {
            statusLabel.text = "已认证"
//            manageSettleBtn.isUserInteractionEnabled = true
//            manageSettleBtn.layer.borderColor = UIColor.THEME.cgColor
//            manageSettleBtn.setTitleColor(.THEME, for: .normal)
            statusBgView.backgroundColor = .THEME
        }else{
            statusLabel.text = "未认证"
//            manageSettleBtn.isUserInteractionEnabled = false
//            manageSettleBtn.layer.borderColor = UIColor.COLOR_LINE_GREY.cgColor
//            manageSettleBtn.setTitleColor(.COLOR_LINE_GREY, for: .normal)
            statusBgView.backgroundColor = .COLOR_LINE_GREY
        }
        manageSettleBtn.isUserInteractionEnabled = true
        manageSettleBtn.layer.borderColor = UIColor.THEME.cgColor
        manageSettleBtn.setTitleColor(.THEME, for: .normal)
//        if dict["status"]as? Int ?? -5 == -2 || dict["status"]as? Int ?? -5 == -1{
//            statusLabel.text = "已驳回"
//        }else if dict["status"]as? Int ?? -5 == 0{
//            statusLabel.text = "待提交"
//        }else if dict["status"]as? Int ?? -5 == 1{
//            statusLabel.text = "审核中"
//        }else if dict["status"]as? Int ?? -5 == 2{
//            statusLabel.text = "待授权"
//        }else if dict["status"]as? Int ?? -5 == 3{
//            statusLabel.text = "已绑定"
//        }else if dict["status"]as? Int ?? -5 == 4{
//            statusLabel.text = "已认证"
//        }
    }
    func updateUIForMerc(dict:NSDictionary) {
        nameLabel.text = "\(dict["merchantName"]as? String ?? "")"
        timeLabel.text = "\(dict["createDate"]as? String ?? "")"
        mercPhoneLabel.text = "\(dict["phone"]as? String ?? "")（\(dict["name"]as? String ?? "")）"
        
        if dict["posStatus"]as? Int ?? -5 == -2 || dict["posStatus"]as? Int ?? -5 == -1{
            statusLabel.text = "已驳回"
        }else if dict["posStatus"]as? Int ?? -5 == 0{
            statusLabel.text = "待提交"
        }else if dict["posStatus"]as? Int ?? -5 == 1{
            statusLabel.text = "审核中"
        }else if dict["posStatus"]as? Int ?? -5 == 2{
            statusLabel.text = "待授权"
        }else if dict["posStatus"]as? Int ?? -5 == 3{
            statusLabel.text = "已绑定"
        }else if dict["posStatus"]as? Int ?? -5 == 4{
            statusLabel.text = "已认证"
        }
        
        arrowImgView.isHidden = true
    }
}

extension ServiceMercTableViewCell{
    func initUI() {
        contentView.addSubview(whiteView)
        whiteView.addSubview(nameLabel)
        whiteView.addSubview(statusBgView)
        whiteView.addSubview(statusLabel)
        whiteView.addSubview(timeLabel)
        whiteView.addSubview(manageSettleBtn)
        whiteView.addSubview(bottomeView)
        
        bottomeView.addSubview(lineView)
        bottomeView.addSubview(mercPhoneLabel)
        bottomeView.addSubview(arrowImgView)
        bottomeView.addSubview(peizhiBtn)
        bottomeView.addSubview(peizhiTapView)
        
        setConstrait()
    }
    func setConstrait() {
        whiteView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(343))
            make.top.equalTo(kFitWidth(10))
            make.bottom.equalToSuperview()
        }
        nameLabel.snp.makeConstraints { make in
            make.left.top.equalTo(kFitWidth(12))
            make.right.equalTo(kFitWidth(-100))
        }
        statusBgView.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(20))
            make.top.equalTo(kFitWidth(-20))
            make.width.equalTo(kFitWidth(100))
            make.height.equalTo(kFitWidth(42))
        }
        statusLabel.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.equalTo(kFitWidth(80))
            make.height.equalTo(kFitWidth(22))
        }
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(14))
            make.top.equalTo(kFitWidth(39))
            make.height.equalTo(kFitWidth(17))
        }
        manageSettleBtn.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(10))
            make.top.equalTo(kFitWidth(64))
            make.width.equalTo(kFitWidth(80))
            make.height.equalTo(kFitWidth(24))
        }
        bottomeView.snp.makeConstraints { make in
            make.left.bottom.width.equalToSuperview()
            make.height.equalTo(kFitWidth(32))
        }
        lineView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(kFitWidth(0.5))
        }
        mercPhoneLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(12))
            make.top.height.equalToSuperview()
        }
        peizhiBtn.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-32))
            make.top.height.equalToSuperview()
            
        }
        arrowImgView.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-12))
            make.centerY.lessThanOrEqualToSuperview()
            make.width.height.equalTo(kFitWidth(20))
        }
        peizhiTapView.snp.makeConstraints { make in
            make.top.right.height.equalToSuperview()
            make.left.equalTo(peizhiBtn)
        }
    }
}
